﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using Microsoft.Win32;

namespace SmokeTester
{
    class Program
    {
        static void Main(string[] args)
        {
            var projectFileXml = XDocument.Load("Samples101.fsproj");

            XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";

            var assemblyName =
                projectFileXml.Root
                    .Element(msbuild + "PropertyGroup")
                    .Element(msbuild + "AssemblyName")
                    .Value + ".dll";

            var outputPaths =
                projectFileXml.Root
                    .Elements(msbuild + "PropertyGroup")
                    .Elements(msbuild + "OutputPath")
                    .Select(x => x.Value);

            foreach (var outputPath in outputPaths)
            {
                string assemblyPath =
                    Path.Combine(outputPath, assemblyName);

                Console.WriteLine(assemblyPath);
            }
        }

        private static void ReadVersion()
        {
            var file =
                new FileInfo(
                    Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "extension.vsixmanifest"));

            var document = XDocument.Load(file.FullName);
            XNamespace ns = "http://schemas.microsoft.com/developer/vsx-schema/2010";
            var version = document.Root.Element(ns + "Identifier").Element(ns + "Version").Value;

            Console.WriteLine(version);
        }

        private static DirectoryInfo CheckGetVisualStudioAddInsDirectory()
        {
            string myDocuments =
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

            if (!Directory.Exists(myDocuments))
                throw new InvalidOperationException(
                    String.Format("My Documents folder at '{0}' was not found.", myDocuments));

            return new DirectoryInfo(myDocuments).CreateSubdirectory(@"Visual Studio 2010\Addins");
        }

        internal static bool TryGetVisualStudioEnvironmentDirectory(out DirectoryInfo environmentDirectory)
        {
            environmentDirectory = null;
            string directory = null;

            try
            {
                var registryKey =
                    Registry.LocalMachine
                        .OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VS");

                if (registryKey != null)
                    directory = registryKey.GetValue("EnvironmentDirectory").ToString();
            }
            catch
            {
                return false;
            }

            if (String.IsNullOrEmpty(directory) || !Directory.Exists(directory))
                return false;

            environmentDirectory = new DirectoryInfo(directory);
            return true;
        }

        private static void UpdateAddInXml()
        {
            var file =
                new FileInfo(
                    Path.Combine(
                        Directory.GetCurrentDirectory(),
                        "AssemblyInfoDialog.AddIn"));

            var document = XDocument.Load(file.FullName);
            XNamespace ns = "http://schemas.microsoft.com/AutomationExtensibility";

            var assembly = document.Root.Element(ns + "Addin").Element(ns + "Assembly");
            assembly.Value = "OtherValue";
            document.Save(file.FullName);
        }

        private static void ReadAsmInfo()
        {
            string file = @"c:\projects\unbound\AssemblyInfoDialog\src\SmokeTester\SomeFolder\SomeSub\SomeOtherFile.cs";

            var directory = new FileInfo(file).Directory;

            while (directory != null && directory.GetFiles("*.csproj", SearchOption.TopDirectoryOnly).Length == 0)
                directory = directory.Parent;

            if (directory == null)
                return;

            var projectFile = directory.GetFiles("*.csproj", SearchOption.TopDirectoryOnly).First();

            var projectFileXml = XDocument.Load(projectFile.FullName);

            XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";

            var assemblyName =
                projectFileXml.Root
                    .Element(msbuild + "PropertyGroup")
                    .Element(msbuild + "AssemblyName")
                    .Value + ".dll";

            var outputPaths =
                projectFileXml.Root
                    .Elements(msbuild + "PropertyGroup")
                    .Elements(msbuild + "OutputPath")
                    .Select(x => x.Value);

            foreach (var outputPath in outputPaths)
            {
                string assemblyPath = 
                    Path.Combine(projectFile.DirectoryName, outputPath, assemblyName);

                if (!File.Exists(assemblyPath))
                    continue;

                var assembly = AssemblyName.GetAssemblyName(assemblyPath);

                Console.WriteLine(assembly.Name);
                Console.WriteLine(assembly.FullName);
                Console.WriteLine(assembly.Version);
                Console.WriteLine(String.Join("", assembly.GetPublicKeyToken().Select(x => x.ToString("x2"))));
                Console.WriteLine(String.Join("", assembly.GetPublicKey().Select(x => x.ToString("x2"))));
                Console.WriteLine(new Uri(assembly.CodeBase).AbsolutePath);

                return;
            }
        }
    }
}
